<template>
	<view class="pageLogin">
		<image src="https://xcxwechat.obs.cn-east-3.myhuaweicloud.com/images/bg-login.png" class="pageCover" mode=""></image>
		<view class="dlxx">
			<image class="dllogo" src="@/static/images/denglu_logo.png" mode=""></image>


			<view v-if="loginType==2" style="margin-top: 138rpx;"
				class="dlxxInput tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
				<view class="content">
					<input v-model="form.phone" maxlength="20" placeholder-class="input-placeholder"
						placeholder="请输入手机号码" />
				</view>
			</view>

			<view v-if="loginType==2"
				class="dlxxInput tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
				<view class="content verify-code">
					<input v-model="form.vcode" placeholder-class="input-placeholder" placeholder="请输入验证码" />
				</view>
				<view class="verify-code">
					<tn-verification-code ref="verificationCode" @change="codeChange"
						countDownText="s秒"></tn-verification-code>
					<tn-button backgroundColor="#F5F5F5" fontColor="#797979" size="sm" padding="5rpx 10rpx" width="100%"
						shape="round" @click="getYzm">{{tips}}</tn-button>
				</view>
			</view>

			<view v-if="loginType==2" class="type2Btn">
				<tn-button backgroundColor="#FFC122" fontColor="#ffffff" height="90rpx" width="100%" shape="round"
					@click="login">确定</tn-button>
			</view>

			<view v-if="loginType==1" class="sjhdl tn-flex tn-flex-row-center tn-flex-col-center">
				<tn-button backgroundColor="#FFC122" padding="40rpx 0" width="590rpx" height="90rpx" :fontSize="30"
					fontColor="#FFFFFF" shape="round" @click="sjhdl">
					<image class="sjimg" src="@/static/images/denglu_sj.png" alt="" />
					<text class="title">手机号登录</text>
				</tn-button>
			</view>
			<view v-if="loginType==1" class="wxdl tn-flex tn-flex-row-center tn-flex-col-center">
				<tn-button v-if="checked && verifyFLag" backgroundColor="#000000" padding="40rpx 0" width="590rpx"
					height="90rpx" :fontSize="30" fontColor="#FFFFFF" shape="round" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber" @click="">
					<image class="wximg" src="@/static/images/denglu_weixin.png" alt="" />
					<text class="title">微信用户授权登录</text>
				</tn-button>
				<tn-button v-if="!checked || !verifyFLag" backgroundColor="#000000" padding="40rpx 0" width="590rpx"
					height="90rpx" :fontSize="30" fontColor="#FFFFFF" shape="round" @click="checkGxxy()">
					<image class="wximg" src="@/static/images/denglu_weixin.png" alt="" />
					<text class="title">微信用户授权登录</text>
				</tn-button>
			</view>

			<view class="yhxy ">
				<tn-checkbox activeColor="#FFC122" v-model="checked" size="24rpx" shape="circle">
					<text class="textInfo">阅读并同意 <text class="xyxx">《用户协议》</text>及 <text class="xyxx">《隐私政策》</text>
					</text>
				</tn-checkbox>
			</view>
		</view>

		<!-- 滑块验证 -->
		<slider-verify :isShow="sliderVerifyFLag" @touchSliderResult="verifyResult" ref="verifyElement"></slider-verify>
	</view>
</template>

<script>
	import {
		loginFast,
		sendSmsCode,
		smsLogin
	} from '@/apis/login.js'
	import {
		setToken
	} from '@/utils/auth';
	export default {
		data() {
			return {
				// 勾选
				checked: false,
				// 按钮状态
				btnFlag: true,
				// 登录方式
				loginType: 1,
				tips: '',
				//滑块
				sliderVerifyFLag: false,
				verifyFLag: false,
				form: {
					phone: '',
					vcode: '',
					code: '',
				}

			};
		},
		created() {
			this.xcxLogin()
		},
		methods: {
			/**
			 * @function 小程序登录
			 */
			xcxLogin() {
				uni.login({
					success: (res) => {
						this.form.code = res.code
					}
				})

			},
			login() {
				if (!this.checked) {
					this.tool.toastTip('请阅读并勾选协议')
					return
				}
				if (!this.verifyFLag) {
					this.sliderVerifyFLag = true
					return
				}
				if (this.loginType == 2) {
					if (this.form.phone && this.form.vcode) {
						smsLogin(this.form).then(res => {
							if (res.code == 0) {
								setToken(res.data.token)
								this.$tn.vuex('vuex_user', res.data)
								this.tool.toastTip('登录成功')
								uni.navigateBack({
									delta: 1
								})
							} else {
								this.tool.toastTip(res.msg)
							}

						})
					}
				}
			},
			/**
			 * @function 勾选协议
			 */
			checkGxxy() {
				if (!this.checked) {
					this.tool.toastTip('请阅读并勾选协议')
				} else {
					this.sliderVerifyFLag = true
				}

			},
			/**
			 * @function 滑块验证
			 */
			verifyResult(res) {
				this.sliderVerifyFLag = false;
				if (res) { //校验通过
					this.verifyFLag = true
				} else {
					// 校验失败,点击关闭按钮
				}
			},
			/**
			 * @function 获取手机号
			 */
			getPhoneNumber(res) {
				if (res.errMsg == 'getPhoneNumber:ok') {
					let param = {
						code: this.form.code, //用于登陆的code ，
						getPhoneCode: res.code, //用于获取手机号 ，
						gender: 1,
						avatar: "123", //用于获取手机号 ，
						nickname: "ceshi", //用于获取手机号 ，
					}
					loginFast(param).then(res => {
						if (res.code == 0) {
							setToken(res.data.token)
							this.$tn.vuex('vuex_user', res.data)
							this.tool.toastTip('登录成功')
							uni.navigateBack({
								delta: 1
							})
						} else {
							this.tool.toastTip(res.msg)
						}
					})
				}

			},
			/**
			 * @function 切换登录方式
			 */
			sjhdl() {
				this.loginType = 2
			},
			/**
			 * @function 切换登录方式
			 */
			codeChange(text) {
				this.tips = text
			},
			/**
			 * @function 获取验证码
			 */
			getYzm() {
				if (this.form.phone) {
					if (this.btnFlag) {
						sendSmsCode({
							phone: this.form.phone
						}).then(res => {
							this.tool.toastTip('验证码发送成功')
							this.btnFlag = false
							if (this.$refs.verificationCode.canGetCode) {
								this.$refs.verificationCode.start()
							}
						})
					}
				} else {
					this.tool.toastTip('请输入手机号')
				}

			}
		}
	}
</script>

<style lang="less">
	.pageLogin {
		min-height: 100vh;

		.dlxx {
			position: absolute;
			align-items: center;
			display: flex;
			flex-flow: column;
			justify-content: center;
			width: 100%;
			min-height: 100%;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 30rpx;

			.dllogo {
				width: 338rpx;
				height: 470rpx;
			}

			.sjhdl {
				margin-top: 80rpx;
				margin-bottom: 40rpx;

				.sjimg {
					width: 36rpx;
					height: 36rpx;
				}

				.title {
					margin-left: 10rpx;
				}
			}

			.wxdl {
				margin-bottom: 35rpx;

				.wximg {
					width: 36rpx;
					height: 36rpx;
				}

				.title {
					margin-left: 10rpx;
				}
			}

			.yhxy {
				padding-left: 60rpx;


				.textInfo {
					font-weight: 400;
					font-size: 24rpx;
					color: #979797;
				}

				.xyxx {
					color: #FFC122;
				}


			}

			.dlxxInput {
				margin-top: 30rpx;
				width: 80%;
				height: 90rpx;
				border-radius: 50rpx;
				background: #F5F5F5;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;

				.content {
					width: 75%;
					padding-left: 40rpx;

					.verify-code {
						width: 56%;
					}

					input {
						font-size: 24rpx;
						// letter-spacing: 0.1em;
					}
				}

			}

			.type2Btn {
				margin-top: 70rpx;
				margin-bottom: 30rpx;
				width: 80%;
			}
		}


	}
</style>